package com.ak.common.utils;

import com.ak.common.entity.AKAuthUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
  * 生成客户端 AuthorizationValue
 * @author Vean
 *
 */
public class AuthorizationUtil {

	/**
	 * 获取登录用户的用户名
	 * @return
	 */
	public static String getUserName() {
		return SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
	}

	/**
	 * 判断某个用户是否当前登录用户
	 * @param userName
	 * @return
	 */
	public static boolean isCurrentUser(String userName) {
		return userName.equals(getUserName());
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		String text = "ak-web:123456";
		byte[] textByte = text.getBytes("UTF-8");
		Base64.Encoder encoder = Base64.getEncoder();
		System.out.println(encoder.encodeToString(textByte));
	}

}
